#include<pthread.h>
#include<iostream>
#include<unistd.h>
#include<vector>
using namespace std;

class Date
{
public:
    Date(int num1,int num2,char op)
        :_num1(num1)
        ,_num2(num2)
        ,_op(op)
    {}

    int _num1;
    int _num2;
    char _op;
};

class Res
{
public:
    Res(int code,int ans)
        :_code(code)
        ,_ans(ans)
    {}

    int _code;
    int _ans;
};

class In
{
public:
    std::vector<Date> datas;
};
class Out
{
public:
    std::vector<Res> results;
};

void* pthreadFun(void* arg)
{
    //pthread_detach(pthread_self()); //设置分离状态
    // int cnt = 5;
    // while(cnt--)
    // {
    //     cout << "111111"<<endl;
    //     sleep(1);
    // }
    In* pi = static_cast<In*> (arg);
    Out* o = new Out;
    
    for(int i = 0;i<pi->datas.size();i++)
    {
        int ans = 0;
        int code = 0;
        int n1 = pi->datas[i]._num1;
        int n2 = pi->datas[i]._num2;
        switch(pi->datas[i]._op)
        {
            case '+':
                ans = n1 + n2;
                break;
            case '-':
                ans = n1 - n2;
                break;
            case '*':
                ans = n1 * n2;
                break;
            case '/':
                if(n2 == 0)
                   code = -1;
                else
                    ans = n1 / n2;
                break;
            default:
                break;
        }
        o->results.push_back({code,ans});
    }

    return o;
}
int main()
{
    pthread_t tid;
    In i;

    i.datas.push_back({1,6,'+'});
    i.datas.push_back({1,6,'-'});
    i.datas.push_back({1,6,'*'});
    i.datas.push_back({6,0,'/'});

    pthread_create(&tid,nullptr,pthreadFun,(void*)&i);
    sleep(1);

    Out* po = nullptr;

    int n = pthread_join(tid,(void**)&po);
    for(int i = 0;i<po->results.size();i++)
    {
        cout << po->results[i]._code << ' ' << po->results[i]._ans << endl;
    }
    cout << "n : " << n << endl;

    return 0;
}
